package com.mygdx.game.group;

import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.mygdx.game.MainGame;
import com.mygdx.game.group.basegroup.BaseGroup;

public class ScoreGroup extends BaseGroup {

    private Image bgImage;
    private int score = 0;
    //用于显示分数的标签
    private Label scoreLabel;

    public ScoreGroup(MainGame mainGame, TextureRegion textureRegion) {
        super(mainGame);
        init(textureRegion);
    }

    public void init(TextureRegion textureRegion) {
        //以背景图片规定宽和高
        setSize(textureRegion.getRegionWidth(), textureRegion.getRegionHeight());

        bgImage = new Image(textureRegion);
        addActor(bgImage);
        Label.LabelStyle style = new Label.LabelStyle();
        style.font = getMainGame().getBitmapFont();

        scoreLabel = new Label(score + "", style);
        scoreLabel.setFontScale(0.4f);
        // 设置标签的宽高（把标签的宽高设置为文本字体的宽高, 即标签包裹文本）
        scoreLabel.setSize(scoreLabel.getPrefWidth(), scoreLabel.getPrefHeight());
        //居中对齐
        scoreLabel.setX(getWidth() / 2 - scoreLabel.getWidth() / 2);
        scoreLabel.setY(18);
        addActor(scoreLabel);
    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
        scoreLabel.setText(score);
        //重新居中
        scoreLabel.setSize(scoreLabel.getPrefWidth(),scoreLabel.getPrefHeight());
        scoreLabel.setX(getWidth() / 2 - scoreLabel.getWidth() / 2);
        scoreLabel.setY(18);
    }

    public void addScore(int scoreStep) {
        setScore(score + scoreStep);
    }
}
